<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var obj = {
        myname: "hello",
        age: 20,
        height: "180cm",
      };
      //myname=hello&age=20&height=180cm

      //传入一个对象,那么返还一个queryString格式的字符串
      function o2s(obj) {
        //获取所有对象的keys
        var keys = Object.keys(obj);
        //console.log(keys); //可以不用for forEach循环去拿到键名  直接Object.keys取出来
        var resArr = keys.map(function (key) {
          return `${key}=${obj[key]}`;
        });
        console.log(resArr);
        var res = resArr.join("&");
        return res;
      }
      o2s(obj);
      console.log(o2s(obj));

      //
      //
      //改成箭头函数

      let o2s = (obj) =>
        Object.keys(obj)
          .map((key) => `${key}=${obj[key]}`)
          .join("&");
      console.log(o2s(obj));
    </script>
  </body>
</html>
